import axios from 'axios';
axios.defaults.baseURL = 'http://www.520gongfu.com:9527/api';
axios.interceptors.request.use(
    config => {
        let token = sessionStorage.getItem('token')
        if (token) {
            config.headers.Authorization = `${token}`;
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    });



//轮播图数据
export const getSliders = (callback) => {
    axios.get('/home/sliders').then(function(response) {
            callback && callback(response.data)
        })
        .catch(function(err) {
            console.log(err);
        })
}

//首页分类的导航
export const homeCategory = (callback) => {
    axios.get('/home/category').then(function(res) {
        callback && callback(res.data)
    }).catch(function(err) {
        console.log(err);
    })
}

//首页楼层
export const homefloorlist = (callback) => {
    axios.get('/home/floorlist').then(function(res) {
        callback && callback(res.data)
    }).catch(function(err) {
        console.log(err);
    })
}

//商品列表页
export const getProcuctList = (params, callback) => {
    return axios.get('/product/list', {
        // get请求参数配置
        params: params, // 参数
        // 序列化参数（格式化处理参数）
        paramsSerializer: function(params) {
            return qs.stringify(params)
        }
    }).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

// 商品详情
export const getProcuctDetail = (params) => {
    return axios.get('/product/detail', {
        params
    }).then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

//分类页数据
export const getcategoryData = () => {
    return axios.get('/category/data').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

//登录请求
import qs from 'qs';
export const login = (params) => {
    return axios.post('/user/login', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

//注册请求
export const register = (params) => {
    return axios.post('/user/register', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

//用户信息获取
export const getUserInfo = () => {
    return axios.get('/user/info').then(res => {
        const { data } = res
        return data
    })
}

//用户页面推荐商品数据
export const getRecommend = () => {
    return axios.get('/product/recommend').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

//退出登录
export const logout = () => {
    return axios.post('/user/logout').then(res => {
        const { data } = res
        return data
    })
}

//获取购物车数据
export const getCartList = () => {
    return axios.get('/cart/list').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            this.$toast.fail(data.msg)
        }
    })
}

//添加商品到购物车
export const submitToCar = (params) => {
    return axios.post('/cart/add', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

//删除购物车数据
export const delCar = (params) => {
    return axios.post('/cart/del', qs.stringify(params)).then(res => {
        const { data } = res
        return data
    })
}

//获取QQ 授权的信息
export const getUserQQinfo = (params, callback) => {
    return axios.get('/user/qqinfo', {
        // get请求参数配置
        params: params, // 参数
        // 序列化参数（格式化处理参数）
        paramsSerializer: function(params) {
            return qs.stringify(params)
        }
    }).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

//     获取 QQ 授权信息之后 获取 当前 应用的登录授权 token
export const getUserQQlogin = (params, callback) => {
    return axios.post('/user/qqlogin', qs.stringify(params)).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}